package com.wfk.h5.controller;

import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.image.common.RestUploadFileInfo;
import com.wfk.constant.PathConstant;
import com.wfk.entity.Address;
import com.wfk.entity.OpinionInfos;
import com.wfk.entity.SysRule;
import com.wfk.h5.mapping.MappingUtils;
import com.wfk.h5.service.FrontService;
import com.wfk.jwt.JWT;
import com.wfk.service.IAddressService;
import com.wfk.service.IBankTypeService;
import com.wfk.service.IBaseInfoService;
import com.wfk.service.IOpinionInfosService;
import com.wfk.service.ISysNewsService;
import com.wfk.service.ISysRuleService;
import com.wfk.utils.Login;
import com.wfk.utils.ResponseData;
import com.wfk.utils.TokenUtils;
import com.wfk.utils.ValidateUtil;

@Controller
public class FrontController {
	
	@Autowired
	private FrontService frontService;
	@Autowired
	private IBankTypeService bankTypeService;
	@Autowired
	private IAddressService addressService;
	@Autowired
	private ISysRuleService sysRuleService;
	@Autowired
	private IOpinionInfosService opinionInfosService;
	@Autowired
	private ISysNewsService sysNewsService;
	@Autowired
	private IBaseInfoService baseInfoService;
	
	
	@RequestMapping(MappingUtils.getBase)
	@ResponseBody
	public ResponseData getBase(String key) {
		try {
			if(!ValidateUtil.equalTo("xinshou", key)&&!ValidateUtil.equalTo("shouyi",key)) {
				return ResponseData.baseError("");
			}
			return ResponseData.ok().putDataValue("result", this.baseInfoService.getByKey(key));
		}catch(Exception e) {
			e.printStackTrace();
			return ResponseData.baseServiceError("网络连接，请重试");
		}
	}
	
	@RequestMapping(MappingUtils.newDetail)
	@ResponseBody
	public ResponseData newDetail(Integer id) {
		try {
			return ResponseData.ok().putDataValue("result", this.sysNewsService.getById(id));
		}catch(Exception e) {
			e.printStackTrace();
			return ResponseData.baseServiceError("网络连接，请重试");
		}
	}
	
	/**
	 * addOpinion
	 * @param vo
	 * @return
	 */
	@RequestMapping(value=MappingUtils.addOpinion)
	@ResponseBody
	public ResponseData addOpinion(OpinionInfos vo){
		try{
			this.opinionInfosService.save(vo);
			return ResponseData.ok();
		}catch(Exception e){
			e.printStackTrace();
			return ResponseData.baseServiceError("内部错误");
		}
	}
	
	@RequestMapping(MappingUtils.sysRule)
	@ResponseBody
	public ResponseData sysRule(String title) {
		try {
			SysRule sysrule = sysRuleService.getOne(new QueryWrapper<SysRule>().eq("title", title));
			return ResponseData.ok().putDataValue("result",sysrule);
		}catch(Exception e) {
			e.printStackTrace();
			return ResponseData.baseServiceError("内部错误");
		}
	}
	/**
	 * 根据pid查询地址
	 * @param pid
	 * @return
	 */
	@RequestMapping(value = MappingUtils.queryaddressbyparentid)
	@ResponseBody
	public ResponseData queryAddressByPid(Integer pid) {
		try{
			return ResponseData.ok().putDataValue("result",addressService.list(new QueryWrapper<Address>().eq("pid",pid)));
		}catch(Exception e){
			e.printStackTrace();
			return ResponseData.baseServiceError("内部错误");
		}
	}
	
	@RequestMapping(MappingUtils.login)
	@ResponseBody
	public ResponseData login(String phone,String password) {
		try {
			return this.frontService.login(phone, password);
		}catch(Exception e) {
			e.printStackTrace();
			return ResponseData.baseServiceError("内部错误");
		}
	}
	
	@RequestMapping(MappingUtils.updateToken)
	@ResponseBody
	public ResponseData updateToken(HttpServletRequest request) {
		try {
			String token = TokenUtils.getToken(request) ;
			//token不存在
			if(null != token) {
				Login login = JWT.unsign(token, Login.class);
				if(null!=login) {
					token = JWT.sign(login,TimeUnit.DAYS.toMillis(1)) ;//加密1天
					return ResponseData.ok().putDataValue("token", token);
				}
			}
			return ResponseData.baseError("登录过期");
		}catch(Exception e) {
			e.printStackTrace();
			return ResponseData.baseServiceError("内部错误");
		}
	}
	
	/**
	 * 
	 * 银行卡类型列表
	 * @return
	 */
	@RequestMapping(MappingUtils.bankTypeList)
	@ResponseBody
	public ResponseData bankTypes(){
		try {
			return ResponseData.ok().putDataValue("result", this.bankTypeService.list(null));
		}catch(Exception e) {
			e.printStackTrace();
			return ResponseData.baseServiceError("内部错误");
		}
	}
	
	@RequestMapping(MappingUtils.download)
	public ModelAndView download() {
		ModelAndView view = new ModelAndView(MappingUtils.download);
		return view;
	}
	
	@RequestMapping(MappingUtils.downloadConverter)
	public ModelAndView downloadConverter() {
		ModelAndView view = new ModelAndView(MappingUtils.downloadConverter);
		view.addObject("title",baseInfoService.getByKey("siteName"));
		return view;
	}
	
	@RequestMapping(MappingUtils.shareUser)
	public ModelAndView shareUser(Integer tjphone) {
		ModelAndView view = new ModelAndView(MappingUtils.shareUser);
		view.addObject("tjphone",tjphone);
		return view;
	}
	
	
	
	@RequestMapping(MappingUtils.register)
	@ResponseBody
	public ResponseData register(String phone,String password,String validcode,Integer parentid){
		try {
			return this.frontService.handleAppRegister(phone,password, parentid,validcode);
		}catch(Exception e) {
			e.printStackTrace();
			return ResponseData.baseServiceError("内部错误");
		}
	} 
	
	/**
	 * 发送修改密码
	 * @param phone
	 * @return
	 */
	@RequestMapping(MappingUtils.sendPassword)
	@ResponseBody
	public ResponseData sendPassword(String phone){
		try{
			return frontService.sendPassword(phone);
		}catch(Exception e){
			e.printStackTrace();
			return ResponseData.baseServiceError("网络连接，请重试");
		}
	}
	
	/**
	 * 发送注册验证码
	 * @param phone
	 * @return
	 */
	@RequestMapping(MappingUtils.sendPhone)
	@ResponseBody
	public ResponseData sendRegister(String phone){
		try{
			return frontService.sendRegister(phone);
		}catch(Exception e){
			e.printStackTrace();
			return  ResponseData.baseServiceError("网络连接，请重试");
		}
	}
	
	/**
	 * <p>单个文件上传</p>
	 * @return
	 */
	@RequestMapping(value=MappingUtils.uploadImg) 
	@ResponseBody
	public ResponseData uploadImg(@RequestParam(value="imgurl",required=false)MultipartFile imgurl){
		String url = "";
		try {
			RestUploadFileInfo r=com.image.common.Service.uploadImage(imgurl.getOriginalFilename(), imgurl.getInputStream(), PathConstant.apphead);
			if(!r.isStatus()) {
				return ResponseData.baseServiceError(r.getDesc());
			}
			url = r.getServiceName()+r.getFilePath()+r.getFileName();
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseData.baseServiceError("上传失败");
		}
		return ResponseData.ok().putDataValue("url", url);
	}
}
